package com.wteam.dragon.model.service.impl;

import cn.hutool.core.lang.Dict;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wteam.dragon.model.mapper.ArticleTypeMapper;
import com.wteam.dragon.model.pojo.bean.ArticleType;
import com.wteam.dragon.model.service.ArticleTypeService;
import com.wteam.dragon.system.basebean.bean.ResultMessage;
import com.wteam.dragon.system.util.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 陈晓辉
 * @Date 2020/4/10 11:07
 */
@Service
@RequiredArgsConstructor
public class ArticleTypeServiceImpl implements ArticleTypeService {
    private final ArticleTypeMapper articleTypeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage addArticleType(ArticleType articleType) {
        articleTypeMapper.insert(articleType);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("添加成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage delArticleType(Long id) {
        ValidationUtil.isNull("id", id);
        articleTypeMapper.deleteById(id);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("删除成功");
        return resultMessage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultMessage editArticleType(ArticleType articleType) {
        articleTypeMapper.updateById(articleType);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setMessage("编辑成功");
        return resultMessage;
    }

    @Override
    public ResultMessage listArticleType(String name) {
        ValidationUtil.isNull("name", name);
        QueryWrapper<ArticleType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(name != null, ArticleType::getName, name)
                .eq(ArticleType::getIsShow, true)
                .orderByAsc(ArticleType::getSort);
        List<ArticleType> articleTypeList = articleTypeMapper.selectList(queryWrapper);
        Dict resultMap = Dict.create()
                .set("message", "请求成功")
                .set("articleTypeList",articleTypeList);
        ResultMessage resultMessage = new ResultMessage();
        resultMessage.setStatus(HttpStatus.OK.value());
        resultMessage.setResultParam(resultMap);
        return resultMessage;
    }
}
